package com.uploadFile.task;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor; /**
 * @Author pl711@qq.com
 * @PackageName uploadFile
 * @Package com.uploadFile.task
 * @Date 2024/5/12 23:30
 * @Version 1.0
 */

@Slf4j
@Component
public class ThreadPoolMonitor extends ThreadPoolTaskExecutor {

    @Value("${async.executor.thread.name.prefix}")
    private String namePrefix;

    @Scheduled(cron = "0/1 * * * * ? ")
    private void showThreadPoolInfo() {
        ThreadPoolExecutor threadPoolExecutor = getThreadPoolExecutor();
        if (namePrefix.equals(this.getThreadNamePrefix())){
            log.info("{} taskCount [{}], completedTaskCount [{}], activeCount [{}], queueSize [{}]",
                    this.getThreadNamePrefix(),
                    threadPoolExecutor.getTaskCount(),
                    threadPoolExecutor.getCompletedTaskCount(),
                    threadPoolExecutor.getActiveCount(),
                    threadPoolExecutor.getQueue().size());
        }
    }
}
